"""用户职称映射类"""

class UserTitleMapping:
    def __init__(self, id=None, user_id=None, business_type=None, 
                 title_name=None, level=None, permissions=None, 
                 grant_date=None, expire_date=None, created_at=None, updated_at=None):
        self.id = id
        self.user_id = user_id
        self.business_type = business_type
        self.title_name = title_name
        self.level = level
        self.permissions = permissions
        self.grant_date = grant_date
        self.expire_date = expire_date
        self.created_at = created_at
        self.updated_at = updated_at


class UserTitleMapper:
    def __init__(self, db_cursor):
        self.cursor = db_cursor

    def insert(self, user_title):
        """插入用户职称映射记录"""
        sql = """
        INSERT INTO user_title_mapping 
        (user_id, business_type, title_name, level, permissions, grant_date, expire_date) 
        VALUES (%s, %s, %s, %s, %s, %s, %s)
        """
        params = (
            user_title.user_id,
            user_title.business_type,
            user_title.title_name,
            user_title.level,
            user_title.permissions,
            user_title.grant_date,
            user_title.expire_date
        )
        self.cursor.execute(sql, params)
        return self.cursor.lastrowid

    def delete_by_user_id_and_business_type(self, user_id, business_type):
        """根据用户ID和业务类型删除记录"""
        sql = "DELETE FROM user_title_mapping WHERE user_id = %s AND business_type = %s"
        self.cursor.execute(sql, (user_id, business_type))
        return self.cursor.rowcount

    def get_titles_by_user_id(self, user_id):
        """根据用户ID获取所有职称"""
        sql = "SELECT * FROM user_title_mapping WHERE user_id = %s"
        self.cursor.execute(sql, (user_id,))
        results = self.cursor.fetchall()
        titles = []
        for result in results:
            titles.append(UserTitleMapping(
                id=result[0],
                user_id=result[1],
                business_type=result[2],
                title_name=result[3],
                level=result[4],
                permissions=result[5],
                grant_date=result[6],
                expire_date=result[7],
                created_at=result[8],
                updated_at=result[9]
            ))
        return titles

    def get_user_business_titles(self, user_id, business_type):
        """根据用户ID和业务类型获取职称"""
        sql = "SELECT * FROM user_title_mapping WHERE user_id = %s AND business_type = %s"
        self.cursor.execute(sql, (user_id, business_type))
        results = self.cursor.fetchall()
        titles = []
        for result in results:
            titles.append(UserTitleMapping(
                id=result[0],
                user_id=result[1],
                business_type=result[2],
                title_name=result[3],
                level=result[4],
                permissions=result[5],
                grant_date=result[6],
                expire_date=result[7],
                created_at=result[8],
                updated_at=result[9]
            ))
        return titles